package com.sfy.sfyrpcspringbootstarter.bootstrap;

import com.sfy.core.example.RpcApplication;
import com.sfy.core.example.config.RpcConfig;
import com.sfy.core.example.service.tcp.VertxTcpServer;
import com.sfy.sfyrpcspringbootstarter.annotation.EnableRpc;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

@Slf4j
public class RpcInitBootstrap implements ImportBeanDefinitionRegistrar  {
    @SneakyThrows
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
                                        BeanDefinitionRegistry registry) {
        boolean needServer = (boolean) importingClassMetadata.
                getAnnotationAttributes(EnableRpc.class.getName()).get("needServer");
        RpcApplication.init();
        // 全局配置
        final RpcConfig rpcConfig = RpcApplication.getRpcConfig();
        if (needServer){
            VertxTcpServer vertxTcpServer = new VertxTcpServer();
            vertxTcpServer.doStart(rpcConfig.getServerPort());
        }else {
            log.info("不启动server");
        }
    }
}
